package com.leyou.auth.web;

import com.leyou.auth.config.OSSProperties;
import com.leyou.auth.dto.AliOssSignatureDTO;
import com.leyou.auth.service.AliAuthService;
import com.leyou.common.constants.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/ali")
public class AuthController {

    @Autowired
    private AliAuthService aliAuthService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OSSProperties prop;

    /**
     * 申请oss签名
     * @return 包含签名、图片验证策略等信息
     */
    @GetMapping("/oss/signature")
    public ResponseEntity<AliOssSignatureDTO> getAliSignature(){
        return ResponseEntity.ok(aliAuthService.getSignature());
    }

    @PostMapping("/oss/callback")
    public ResponseEntity<String> saveImage(@RequestParam("filename") String filename) {
        filename = prop.getHost().concat("/").concat(filename);
        redisTemplate.opsForSet().add(RedisConstant.BRAND_IMAGE, filename);
        return ResponseEntity.ok("ok");
    }
}
